//
//  CRequestCommand.m
//  collection
//
//  Created by 张靖 on 11-12-5.
//  Copyright (c) 2011年 collection. Inc.. All rights reserved.
//

#import "CRequestCommand.h"
#import "CStopWatch.h"
#import "CWorkedThread.h"

@interface CRequestCommand ()

@property (retain) CURLLoaderProxy* urlDataLoaderProxy;
@property (retain) NSDictionary* postData;
@property (retain) NSDictionary* header;
@property (retain) NSString* url;
@property (assign) BOOL start;
@property (retain) CStopWatch* stopWatch;

@end

@implementation CRequestCommand

@synthesize urlDataLoaderProxy = _urlDataLoaderProxy;
@synthesize postData = _postData;
@synthesize header = _header;
@synthesize url = _url;
@synthesize start = _start;
@synthesize httpErrorCode = _httpErrorCode;
@synthesize delegate = _delegate;
@synthesize stopWatch = _stopWatch;
@synthesize responseCode = _responseCode;
@synthesize commandID = _commandID;

- (id) init
{
	self = [super init];
	if (self)
	{
		self.urlDataLoaderProxy = [[[CURLLoaderProxy alloc] init] autorelease];
		self.urlDataLoaderProxy.delegate = self;
		self.postData = nil;
		self.header = nil;
		self.url = nil;
		self.httpErrorCode = eURLErrorNone;
		self.start = NO;
		self.delegate = nil;
//		self.stopWatch = [CStopWatch stopwatch];
	}
	return self;
}

- (id) initWithDelegate:(id<CRequestCommandDelegate>)delegate
{
	self = [self init];
	if (self)
	{
		self.delegate = delegate;
	}
	return self;
}

- (void) startRequest:(NSString*)url postData:(NSDictionary*)postData
{
	[self startRequest:url postData:postData header:nil];
}

- (void) startRequest:(NSString*)url postData:(NSDictionary*)postData header:(NSDictionary*)header
{
	if ([self isRequesting]) return;
    
    //NSLog(@"%@\n", url);
    
	self.start = YES;
	self.httpErrorCode = eURLErrorNone;
	self.url = url;
	self.postData = postData;
	self.header = header;
	
	[self.stopWatch startWatch];
	[[CWorkedThread sharedWorkedThread] performTarget:self selector:@selector(startRequestInThread) withObject:self];
//	辅助线程测试
//	[self startRequestInThread];
}

- (void) startRequestInThread
{
	[self.stopWatch lap:@"1、主线程切换到辅助线程时间"];
	if ([self.urlDataLoaderProxy isLoading]) return;
	
	[self.stopWatch startWatch];
	if (self.postData != nil)
	{
		[self.urlDataLoaderProxy loadMultiDataStart:self.url postData:self.postData header:self.header];
	}
	else 
	{
		[self.urlDataLoaderProxy loadDataStart:self.url header:self.header];
	}
}

- (BOOL) isRequesting
{
	return self.start || [self.urlDataLoaderProxy isLoading];
}

- (void) stopRequest
{
	if (self.start)
	{
		self.start = NO;
//		[[CWorkedThread sharedWorkedThread] performTargetSync:self selector:@selector(stopRequestInThread) withObject:self];
		[[CWorkedThread sharedWorkedThread] performTarget:self selector:@selector(stopRequestInThread) withObject:self];
	}
}

- (void) stopRequestInThread
{
	[self.urlDataLoaderProxy cancelLoadData];
}

- (void) parserDataInThread:(NSData*)recvData
{
}

- (void) urlDataLoadComplete:(CURLDataLoader*)loader
{
	[self.stopWatch lap:[NSString stringWithFormat:@"2、网络请求时间: %@", loader.urlStr]];
	XLOG(@"OK");
//	XLOGDATASTR(loader.urlData);
	self.httpErrorCode = eURLErrorNone;
	[self.stopWatch startWatch];
	[self parserDataInThread:loader.urlData];
	[self.stopWatch lap:@"3、辅助线程中解析数据时间"];
	[self.stopWatch startWatch];
	[self performSelectorOnMainThread:@selector(__requestCompleteInMainThread) withObject:nil waitUntilDone:NO];
}

- (void) __requestCompleteInMainThread
{
	[self.stopWatch lap:@"4、从线程切换回主线程时间"];
	[self.stopWatch startWatch];
	if (self.start)
	{
		self.start = NO;
		[self requestCompleteInMainThread];
	}
	[self.stopWatch lap:@"5、主线程处理数据时间"];
}

- (void) requestCompleteInMainThread
{
	if (self.delegate != nil)
	{
		[self.delegate requestComplete:self];
	}
}

- (void) urlDataLoadError:(CURLDataLoader*)loader errorCode:(TUrlDataLoadErrorCode)errorCode
{
	XLOG(@"Error: %d", errorCode);
	self.httpErrorCode = errorCode;
	[self performSelectorOnMainThread:@selector(requestErrorOccorredInMainThread) withObject:nil waitUntilDone:NO];
}

- (void) requestErrorOccorredInMainThread
{
	self.start = NO;
	NSNotification* notif = [NSNotification notificationWithName:kHttpRequestErrorNotification object:[NSNumber numberWithInt:self.httpErrorCode] userInfo:nil];
	[[NSNotificationCenter defaultCenter] postNotification:notif];
	if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestErrored:)])
	{
		[self.delegate requestErrored:self];
	}
}

- (void) dealloc
{
	self.urlDataLoaderProxy.delegate = nil;
	self.urlDataLoaderProxy = nil;
	self.postData = nil;
	self.header = nil;
	self.url = nil;
	self.delegate = nil;
	self.stopWatch = nil;
	
	[super dealloc];
}

//- (NSString*) description
//{
//	return [NSString stringWithFormat:@"cmd:%d, code:%d, data:%@", self.commandID, self.responseCode, self.responseDic];
//}

- (NSString*) description
{
	return [NSString stringWithFormat:@"cmd:%d, code:%d", self.commandID, self.responseCode];
}

@end
